package com.coffee.admin.service.merchant.impl;


import com.coffee.admin.api.merchant.query.MerchantInvoiceQuery;
import com.coffee.admin.api.merchant.result.MerchantInvoiceResult;
import com.coffee.admin.api.merchant.service.MerchantInvoiceService;
import com.coffee.admin.service.merchant.bean.MerchantInvoice;
import com.coffee.admin.service.merchant.dao.MerchantInvoiceDao;
import com.coffee.core.entity.Page;
import com.coffee.core.entity.PagerInfo;
import com.coffee.core.enums.ExceptionEnum;
import com.coffee.core.exception.BusinessException;
import com.coffee.core.util.BeanUtilExt;
import com.coffee.core.util.DTOUtils;
import com.coffee.core.util.StringUtil;
import net.logstash.logback.encoder.org.apache.commons.lang.StringUtils;
import org.apache.dubbo.config.annotation.DubboService;

import javax.annotation.Resource;
import java.util.*;


@DubboService(dynamic = true)
public class MerchantInvoiceServiceImpl implements MerchantInvoiceService {
    @Resource
    private MerchantInvoiceDao merchantInvoiceDao;

    @Override
    public Page<MerchantInvoiceResult> getPager(MerchantInvoiceQuery param, PagerInfo pr) {
        Page<MerchantInvoiceResult> pager = new Page<MerchantInvoiceResult>(pr.getStart(), pr.getPageSize());
        Integer count = merchantInvoiceDao.count(param);
        pager.setTotalPage(count);
        if (count > 0) {
            List<MerchantInvoice> list = merchantInvoiceDao.page(param, pr.getStart(), pr.getPageSize());
            pager.setData(DTOUtils.convertList(list, MerchantInvoiceResult.class));
        }
        return pager;
    }

    @Override
    public Integer count(MerchantInvoiceQuery param) {
        Integer count = merchantInvoiceDao.count(param);
        return count;
    }

    @Override
    public MerchantInvoiceResult getDetail(Integer id, Integer merchantId) {
        MerchantInvoice item = merchantInvoiceDao.findByIdAndMerchantId(id, merchantId);
        if (item == null) {
            return null;
        }
        MerchantInvoiceResult result = new MerchantInvoiceResult();
        BeanUtilExt.copyProperties(result, item);

        return result;
    }

    @Override
    public List<MerchantInvoiceResult> getList(MerchantInvoiceQuery param, PagerInfo pr) {
        List<MerchantInvoice> list = merchantInvoiceDao.page(param, pr.getStart(), pr.getPageSize());
        return DTOUtils.convertList(list, MerchantInvoiceResult.class);
    }

    @Override
    public List<MerchantInvoiceResult> getList(MerchantInvoiceQuery param) {
        List<MerchantInvoice> list = merchantInvoiceDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, MerchantInvoiceResult.class);
    }

    @Override
    public List<MerchantInvoiceResult> getList() {
        MerchantInvoiceQuery param = new MerchantInvoiceQuery();
        List<MerchantInvoice> list = merchantInvoiceDao.page(param, 0, Integer.MAX_VALUE);
        return DTOUtils.convertList(list, MerchantInvoiceResult.class);
    }


    @Override
    public void delete(Integer id, Integer merchantId) {
        merchantInvoiceDao.deleteByIdAndMerchantId(id, merchantId);
    }

    @Override
    public void addOrUpdate(MerchantInvoiceResult result) throws BusinessException {
        MerchantInvoice item = new MerchantInvoice();
        BeanUtilExt.copyProperties(item, result);
        if (item.getId() == null) {
            if (StringUtils.isNotBlank(result.getIsDefault()) && "Y".equals(result.getIsDefault())) {
                merchantInvoiceDao.updateIsDefaultByMerchantId(item.getMerchantId());
            }
            item.setCreateTime(new Date());
            merchantInvoiceDao.insert(item);
        } else {
            MerchantInvoice tmp = merchantInvoiceDao.findById(item.getId());
            if (tmp == null) {
                throw new BusinessException(ExceptionEnum.ERROR_0002.getCode(), ExceptionEnum.ERROR_0002.getMessage());
            }
            if (StringUtils.isNotBlank(result.getIsDefault()) && "Y".equals(result.getIsDefault())) {
                merchantInvoiceDao.updateIsDefaultByMerchantId(item.getMerchantId());
            }
            merchantInvoiceDao.updateById(item);
        }
    }

    @Override
    public Map<Integer, MerchantInvoiceResult> getItemMap(Set<Integer> idSet) {
        Map<Integer, MerchantInvoiceResult> map = new HashMap<Integer, MerchantInvoiceResult>();
        List<MerchantInvoice> list = merchantInvoiceDao.findByIds(StringUtil.setToList(idSet));
        List<MerchantInvoiceResult> resultList = DTOUtils.convertList(list, MerchantInvoiceResult.class);
        for (MerchantInvoiceResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }

    @Override
    public Map<Integer, MerchantInvoiceResult> getItemMap() {
        Map<Integer, MerchantInvoiceResult> map = new HashMap<Integer, MerchantInvoiceResult>();
        MerchantInvoiceQuery param = new MerchantInvoiceQuery();
        List<MerchantInvoice> list = merchantInvoiceDao.page(param, 0, Integer.MAX_VALUE);
        List<MerchantInvoiceResult> resultList = DTOUtils.convertList(list, MerchantInvoiceResult.class);
        for (MerchantInvoiceResult item : resultList) {
            map.put(item.getId(), item);
        }
        return map;
    }


}
